<?php
// +----------------------------------------------------------------------
// | 框架公共入口文件
// +----------------------------------------------------------------------

//框架需要运行于PHP5.0版本之上
if (version_compare(PHP_VERSION, '5.0.0', '<'))
	die('require PHP > 5.0');
if (version_compare(PHP_VERSION, '6.0.0', '<')) {
	@set_magic_quotes_runtime(0);
	define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc() ? true : false);
}
//记录开始运行时间
$GLOBALS['sys_time_start'] = microtime(true);

// 记录内存初始使用
define('IS_MEMORY', function_exists('memory_get_usage'));
if (IS_MEMORY)
	$GLOBALS['sys_usemem_start'] = memory_get_usage();

//获取配置值
function C($name=null, $value=null) {
	static $_config = array();
	if (empty($name))
		return $_config;

	// 优先执行设置获取或赋值
	if (is_string($name)) {
		if (!strpos($name, '.')) {
			$name = strtolower($name);
			if (is_null($value))
				return isset($_config[$name]) ? $_config[$name] : null;
			$_config[$name] = $value;
			return;
		}
		// 二维数组设置和获取支持
		$name = explode('.', $name);
		$name[0] = strtolower($name[0]);
		if (is_null($value))
			return isset($_config[$name[0]][$name[1]]) ? $_config[$name[0]][$name[1]] : null;
		$_config[$name[0]][$name[1]] = $value;
		return;
	}
	// 批量设置
	if (is_array($name))
		return $_config = array_merge($_config, array_change_key_case($name));
	return null;
}

// 系统目录定义
define('FEE_PATH', dirname(__FILE__));
if (!defined('APP_PATH'))  define('APP_PATH', dirname($_SERVER['SCRIPT_FILENAME']));
if (!defined('DATA_PATH'))  define('DATA_PATH', APP_PATH . '/data');
define('TPL_DIR', 'tpl'); //模板目录
define('CACHE_DIR', 'cache'); //缓存目录
define('LOG_DIR', 'log'); //日志目录
define('PLUGIN_DIR', 'plugin'); //插件目录

//系统路径定义
define('CACHE_PATH', DATA_PATH . '/' . CACHE_DIR); //公共缓存路径
define('LOG_PATH', DATA_PATH . '/' . LOG_DIR); //项目日志路径
define('COMMON_PATH', APP_PATH . '/_common'); //项目公共路径
define('PLUGIN_PATH', FEE_PATH . '/' . PLUGIN_DIR); //框架插件路径

//系统运行环境检测
define('IS_CGI', substr(PHP_SAPI, 0, 3) == 'cgi' ? 1 : 0);
define('IS_CLI',PHP_SAPI=='cli'? 1   :   0);
define('IS_WIN', strstr(PHP_OS, 'WIN') ? 1 : 0);

// 当前文件名
if (IS_CGI) {
	//CGI/FASTCGI模式下
	$_temp = explode('.php', $_SERVER["PHP_SELF"]);
	define('_PHP_FILE_', rtrim(str_replace($_SERVER["HTTP_HOST"], '', $_temp[0] . '.php'), '/'));
} else {
	define('_PHP_FILE_', '/'.trim($_SERVER["SCRIPT_NAME"], '/'));
}

if (!defined('_ROOT_')) {
	// 网站URL根目录
	$_root = dirname(_PHP_FILE_);
	define('_ROOT_', (($_root == '/' || $_root == '\\') ? '' : $_root));
}

//应用名称
if (!defined('APP_NAME')) {
	$_app = str_replace(dirname(APP_PATH), '', APP_PATH);
	define('APP_NAME', trim($_app,"/"));
}

//加载系统扩展函数
require FEE_PATH . '/function/extend.fun.php';

//加载系统核心函数
require FEE_PATH . '/function/function.fun.php';

//加载兼容函数
if(version_compare(PHP_VERSION,'5.2.0','<')){
	require FEE_PATH . '/function/compat.fun.php';
}

// 加载系统配置文件
C(require FEE_PATH . '/config/config.inc.php');

//加载模块公共配置文件
if(is_file(COMMON_PATH . '/config.inc.php')){
	C(require COMMON_PATH . '/config.inc.php');
}

//加载扩展配置文件
$config_list = C('sys_config_list');
foreach ($config_list as $val){
	if(is_file(COMMON_PATH . '/'.$val.'.inc.php')){
		C('_'.$val.'_',array_change_key_case(include COMMON_PATH . '/'.$val.'.inc.php'));
	}
}

//加载项目公共函数库
if(is_file(COMMON_PATH . '/function.fun.php')){
	C(require COMMON_PATH . '/function.fun.php');
}

//加载框架核心类库
require FEE_PATH . '/core/fee.class.php'; //加载核心基类
require FEE_PATH . '/core/fee_exception.class.php'; //加载异常处理类
require FEE_PATH . '/core/log.class.php'; //加载日志类
require FEE_PATH . '/core/app.class.php'; //应用程序类
require FEE_PATH . '/core/controller.class.php'; //控制器类
require FEE_PATH . '/core/model.class.php'; //模型类
require FEE_PATH . '/core/view.class.php'; //视图类
require FEE_PATH . '/core/dispatcher.class.php'; //加载url调度器

//加载别名
alias_import(array(
	'model' => FEE_PATH . '/core/model.class.php',
	'view_model' => FEE_PATH . '/core/model/view_model.class.php',
	'adv_model' => FEE_PATH . '/core/model/adv_model.class.php',
	'relation_model' => FEE_PATH . '/core/model/relation_model.class.php',
	'db' => FEE_PATH . '/classes/db/db.class.php',
	'template' => FEE_PATH . '/classes/template/template.class.php',
	'taglib' => FEE_PATH . '/classes/template/taglib.class.php',
	'taglib_cx' => FEE_PATH . '/classes/template/taglib/taglib_cx.class.php',	
	'cache' => FEE_PATH . '/classes/cache/cache.class.php',
	'debug' => FEE_PATH . '/classes/debug.class.php',

	'behavior' => FEE_PATH . '/classes/behavior/behavior.class.php',
	'html_cache' => FEE_PATH . '/classes/html_cache.class.php',	
	'cookie' => FEE_PATH . '/classes/cookie.class.php',
	'session' => FEE_PATH . '/classes/session.class.php',
	'files' => FEE_PATH . '/classes/files.class.php',
	'form' => FEE_PATH . '/classes/form.class.php',
	'image' => FEE_PATH . '/classes/image.class.php',
	'input' => FEE_PATH . '/classes/input.class.php',
	'mail' => FEE_PATH . '/classes/mail.class.php',
	'page' => FEE_PATH . '/classes/page.class.php',	
	'string' => FEE_PATH . '/classes/string.class.php',
	'tree' => FEE_PATH . '/classes/tree.class.php',
	'upload_file' => FEE_PATH . '/classes/upload_file.class.php',
	'widget' => FEE_PATH . '/classes/widget.class.php',
	
));

//还原命令行模式参数
if (IS_CLI) {
	$_argv = $argv;
	array_shift($_argv); //去掉第一个无用参数
	if(isset($_argv[0])){
		$_act = explode('.',array_shift($_argv));
		switch (count($_act)) {
			case 1:
				array_unshift($_act, C('sys_default_module'), C('sys_default_controller'));
				break;
			case 2:
				array_unshift($_act, C('sys_default_module'));
				break;
			default:
				break;
		}
		$_get = array(C('var_module')=>$_act[0], C('var_controller')=>$_act[1], C('var_action')=>$_act[2]);
	}else{
		$_get = array(C('var_module')=>C('sys_default_module') ,C('var_controller')=>C('sys_default_controller'), C('var_action')=>C('sys_default_action'));
	}
	$_GET = array_merge($_GET,$_get);
	preg_replace('@(\w+)\/([^,\/]+)@e', '$_GET[\'\\1\']="\\2";', implode('/',$_argv)); //还原$_GET参数
}

//执行url控制器
dispatcher::dispatch();

//设置系统时区 PHP5支持
if (function_exists('date_default_timezone_set'))  date_default_timezone_set(C('sys_default_timezone'));

//模块路径
define('MODULE_PATH', APP_PATH . '/' . MODULE_NAME);
//控制器路径
define('CONTROLLER_PATH', MODULE_PATH . '/' . 'controller');
//模型路径
if (!defined('MODEL_PATH'))  define('MODEL_PATH', MODULE_PATH . '/model');
//模板路径
if (!defined('TPL_PATH'))  define('TPL_PATH', APP_PATH . '/' . MODULE_NAME . '/' . TPL_DIR);
define('REFERER', isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '');
define('IP', get_ip());
define('TIME', time());

// 记录加载文件时间
$GLOBALS['sys_time_load'] = microtime(TRUE);
define('FEE_VERSION', '1.0');